<template>
  <el-row class="bg-content">
    <el-col :span="24" style="text-align: right;line-height: 60px;">
      <el-button type="info" @click="addPlan">新建计划</el-button>
    </el-col>
    <el-col :span="24">
      <el-table
        :data="tableData"
        v-loading='loading_plan'
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        ref="tableList"
        style="width: 100%">
        <template v-for="item in table_header">
          <el-table-column
            show-overflow-tooltip
            align="center"
            :width="item['width']"
            :key="item['key']"
            :label="item['label']"
            :prop="item['prop']"
            :column-key="item['columnKey']"
            :filters="item['filters']"
            v-if="show_header_fields.includes(item['key'])"
          ></el-table-column>
        </template>
        <el-table-column
          label="操作"
          align="center"
          width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="deletePlan(scope.row.id)">删除</el-button>
            <el-button type="text" @click="editPlan(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <plan-content :visible.sync="dialogVisible" :post_form="post_form" ref="ref_content_plan" :title="content_title" @fetchData="fetchData"></plan-content>
  </el-row>
</template>


<script>
import { table_header, show_header_fields } from "./index.data"
import planContent from "./content"
import { initial_post_form } from "./content.data"
import { testPlanList, delTestPlan } from "@/api/testPlan"

export default {
  components: {
    planContent,
  },
  data() {
    return {
      table_header,
      show_header_fields,
      tableData: [],
      loading_plan: false,
      // 测试计划详情
      initial_post_form,
      content_title: '',
      post_form: JSON.parse(JSON.stringify(initial_post_form)),
      dialogVisible: false,
    }
  },
  methods: {
    // 获取初始数据
    fetchData() {
      this.loading_plan = true
      testPlanList().then(res => {
        this.loading_plan = false
        this.tableData = res.data
      }, err => {
        this.loading_plan = false
      })
    },
    // 新建计划
    addPlan() {
      this.content_title = '新建计划'
      this.post_form = JSON.parse(JSON.stringify(initial_post_form))
      this.$nextTick(()=>{
        this.$refs['ref_content_plan'].clearFormValidate()
      })
      this.dialogVisible = true
    },
    deletePlan(id) {
      delTestPlan({'id': id}).then(res => {
        this.fetchData()
      })
    },
    // 编辑计划
    editPlan(row) {
      this.content_title = '编辑计划'
      this.post_form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped lang="scss">
/* 重写table样式 */
.bg-content {
  ::v-deep .el-table {
    background-color: #0000000d;
    color: #fff;

    th, tr, td {
      background-color: #0000000d;
    }

    :hover>td { 
      background-color: #ffffff44 !important;
    }

    i {
      color: #fff;
    }
  }
}
</style>

